/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.check.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.suli.check.domain.CheckRecord;
import me.zhengjie.suli.check.service.CheckRecordService;
import me.zhengjie.suli.check.service.dto.CheckRecordQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Allen Gao
* @date 2024-04-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "抽检记录管理")
@RequestMapping("/api/checkRecord")
public class CheckRecordController {

    private final CheckRecordService checkRecordService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('checkRecord:list')")
    public void exportCheckRecord(HttpServletResponse response, CheckRecordQueryCriteria criteria) throws IOException {
        checkRecordService.download(checkRecordService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询抽检记录")
    @ApiOperation("查询抽检记录")
    @PreAuthorize("@el.check('checkRecord:list')")
    public ResponseEntity<Object> queryCheckRecord(CheckRecordQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(checkRecordService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增抽检记录")
    @ApiOperation("新增抽检记录")
    @PreAuthorize("@el.check('checkRecord:add')")
    public ResponseEntity<Object> createCheckRecord(@Validated @RequestBody CheckRecord resources){
        return new ResponseEntity<>(checkRecordService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改抽检记录")
    @ApiOperation("修改抽检记录")
    @PreAuthorize("@el.check('checkRecord:edit')")
    public ResponseEntity<Object> updateCheckRecord(@Validated @RequestBody CheckRecord resources){
        checkRecordService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除抽检记录")
    @ApiOperation("删除抽检记录")
    @PreAuthorize("@el.check('checkRecord:del')")
    public ResponseEntity<Object> deleteCheckRecord(@RequestBody String[] ids) {
        checkRecordService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/addOutBoundCheck")
    @Log("新增抽检任务")
    @ApiOperation("新增抽检任务")
    public ResponseEntity<Object> addOutBoundCheck(@RequestBody String param) {
        return new ResponseEntity<>(checkRecordService.addOutBoundCheck(param),HttpStatus.OK);
    }

    @PostMapping("/pointCheckReturn")
    @Log("抽检回库")
    @ApiOperation("抽检回库")
    public ResponseEntity<Object> pointCheckReturn(@RequestBody String param) {

        return new ResponseEntity<>(checkRecordService.pointCheckReturn(param),HttpStatus.OK);
    }
}